package org.kl.wx.web;

import java.util.List;
import java.util.Map;

import javax.servlet.ServletRequest;

import org.kl.bf.entity.easyui.EasyuiGrid;
import org.kl.bf.utils.KLServlets;
import org.kl.bf.web.base.BasicController;
import org.kl.wx.entity.App;
import org.kl.wx.entity.Groups;
import org.kl.wx.entity.WxUser;
import org.kl.wx.service.AppService;
import org.kl.wx.service.GroupsService;
import org.kl.wx.service.WxUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springside.modules.web.MediaTypes;

@Controller
@RequestMapping(value = "/wx/wxManages")
public class WxManagesController extends BasicController{
	@Autowired
	private AppService appService;
	@Autowired
	private GroupsService groupsService;
	@Autowired
	private WxUserService wxUserService;
	
	@RequestMapping(method = RequestMethod.GET)
	public String list(){
		return "wx/wxManagesList";
	}
	
	@RequestMapping(value = "appDataGrid", produces = MediaTypes.JAVASCRIPT_UTF_8)
	@ResponseBody
	public String appDataGrid(@RequestParam(value = "pageNumber", defaultValue = "1") int pageNumber,
			@RequestParam(value = "rows", defaultValue = PAGE_SIZE) int pageSize,
			@RequestParam(value = "sort", defaultValue = "id") String sortType,
			@RequestParam(value = "order", defaultValue = "desc") String orderType, Model model, ServletRequest request) {
		Map<String, Object> searchParams = KLServlets.getParametersStartingWith(request, SEARCH_PREFIX);
		Page<App> app = appService.getApp(searchParams, pageNumber, pageSize, sortType,orderType);
		EasyuiGrid grid = new EasyuiGrid();
		grid.setRows(app.getContent());
		grid.setTotal((int) app.getTotalElements());
		String result = jsonMapper.toJson(grid);
		return result;
	}
	
	@RequestMapping(value = "groupsDataGrid", produces = MediaTypes.JAVASCRIPT_UTF_8)
	@ResponseBody
	public String groupsDataGrid(
			@RequestParam(value = "sort", defaultValue = "id") String sortType,
			@RequestParam(value = "order", defaultValue = "desc") String orderType, Model model, ServletRequest request) {
		Map<String, Object> searchParams = KLServlets.getParametersStartingWith(request, SEARCH_PREFIX);
		List<Groups> groups = groupsService.getAllGroups(searchParams, sortType,orderType);
		EasyuiGrid grid = new EasyuiGrid();
		grid.setRows(groups);
		grid.setTotal(groups.size());
		String result = jsonMapper.toJson(grid);
		return result;
	}
	
	@RequestMapping(value = "wxUserDataGrid", produces = MediaTypes.JAVASCRIPT_UTF_8)
	@ResponseBody
	public String wxUserDataGrid(
			@RequestParam(value = "sort", defaultValue = "id") String sortType,
			@RequestParam(value = "order", defaultValue = "desc") String orderType, Model model, ServletRequest request) {
		Map<String, Object> searchParams = KLServlets.getParametersStartingWith(request, SEARCH_PREFIX);
		List<WxUser> wxUser = wxUserService.getAllWxUser(searchParams, sortType,orderType);
		EasyuiGrid grid = new EasyuiGrid();
		grid.setRows(wxUser);
		grid.setTotal(wxUser.size());
		String result = jsonMapper.toJson(grid);
		return result;
	}
	
	@ModelAttribute
	public void getApp(@RequestParam(value = "id", defaultValue = "-1") String id, Model model) {
		if (!id.equals("-1")) {
			model.addAttribute("app", appService.getApp(id));
		}
	}

}
